/*
 * Copyright 2008 Carnegie Mellon University
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, 
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package edu.cmu.lti.sepia.infra.topic;

import java.util.ArrayList;
import java.util.List;

import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;

import edu.cmu.lti.sepia.data.xml.IXmlHandler;
import edu.cmu.lti.sepia.infra.topic.model.Answer;
import edu.cmu.lti.sepia.infra.topic.model.Nugget;
import edu.cmu.lti.sepia.infra.topic.model.Topic;
import edu.cmu.lti.sepia.infra.topic.model.TopicSet;
import edu.cmu.lti.sepia.infra.topic.model.Vote;

public class GoldStandardXmlToObj extends DefaultHandler implements IXmlHandler {
	
  private String srcLang;
  private String trgLang;
  
  public GoldStandardXmlToObj(String srcLang, String trgLang) {
    this.srcLang = srcLang;
    this.trgLang = trgLang;
  }
  
	enum XML {
		TOPIC, ID, TITLE, QUESTION, DESCRIPTION, ANSWERTYPE, NARRATIVE, LANG, TEXT, DOCNO, NUGGET, VITAL, NONVITAL
	}
	
	private TopicSet result = new TopicSet();
		
	private String id;
	private String atype;
	private String qSrc;
	private String qTrg;
	private String nSrc;
	private String nTrg;
	private String title;
	private List<Answer> answers = new ArrayList<Answer>();
  private List<Nugget> nuggets = new ArrayList<Nugget>();
  
  @SuppressWarnings("unused")
  private String tempId;
  private String tempDocid;
  private int tempVital;
  private int tempNonVital;
  
	private String currentLang;
	private boolean inQuestion;
	private boolean inAtype;
	private boolean inNarrative;
	private boolean inAnswerText;
	private boolean inAnswerNugget;
	
  private StringBuilder buffer = new StringBuilder();
	
	@Override
	public void startElement( String uri, String localName, 
			String qName, Attributes attributes ) {
		if ( XML.TOPIC.toString().equalsIgnoreCase(qName) ) {
			id = attributes.getValue(XML.ID.toString());
			title = attributes.getValue(XML.TITLE.toString());
		}
		if ( XML.QUESTION.toString().equalsIgnoreCase(qName) || XML.DESCRIPTION.toString().equalsIgnoreCase(qName)  ) {
      inQuestion = true;
      buffer = new StringBuilder();
		  currentLang = attributes.getValue(XML.LANG.toString());
		}
		if ( XML.NARRATIVE.toString().equalsIgnoreCase(qName) ) {
      inNarrative = true;
      buffer = new StringBuilder();
      currentLang = attributes.getValue(XML.LANG.toString());
    }
		if ( XML.ANSWERTYPE.toString().equalsIgnoreCase(qName) ) {
		  inAtype = true;
		  buffer = new StringBuilder();
		}
		if ( XML.TEXT.toString().equalsIgnoreCase(qName) ) {
		  inAnswerText = true;
      buffer = new StringBuilder();
		  tempId = attributes.getValue(XML.ID.toString());
		  tempDocid = attributes.getValue(XML.DOCNO.toString());
		}
		if ( XML.NUGGET.toString().equalsIgnoreCase(qName) ) {
		  inAnswerNugget = true;
      buffer = new StringBuilder();
		  tempId = attributes.getValue(XML.ID.toString());
		  try {
		    tempVital = Integer.parseInt(attributes.getValue(XML.VITAL.toString()));
      } catch (Exception e) {}
      try {
        tempNonVital = Integer.parseInt(attributes.getValue(XML.NONVITAL.toString()));
      } catch (Exception e) {}
		}
	}
	
  @Override
	public void characters(char[] ch, int offset, int length) {
	  String value = null;
	  if ( inQuestion||inNarrative||inAtype||inAnswerText||inAnswerNugget ) {
	    value = new String(ch, offset, length);
	    buffer.append(value);
	  }
	}
	
	@Override
	public void endElement( String uri, String localName, String qName ) {
	  String value = buffer.toString();
    if ( inQuestion && currentLang!=null ) {
      if (currentLang.equalsIgnoreCase(srcLang)) {
        qSrc = value;
      }
      if (currentLang.equalsIgnoreCase(trgLang)) {
        qTrg = value;  
      }
    }
    if ( inNarrative && currentLang!=null ) {
      if (currentLang.equalsIgnoreCase(srcLang)) {
        nSrc = value;
      } else if (currentLang.equalsIgnoreCase(trgLang)) {
        nTrg = value;
      }
    }
    if ( inAtype ) {
      atype = value;
    }
    if ( inAnswerText ) {
      Answer a = new Answer(0,0,tempDocid,0,0,value,value,"");
      answers.add(a);
    }
    if ( inAnswerNugget ) {
      Nugget n = new Nugget(0,0,value,false);
      List<Vote> votes = new ArrayList<Vote>();
      for (int i=0; i<tempVital; i++) {
        votes.add(new Vote(0,0,true));
      }
      for (int i=0; i<tempNonVital; i++) {
        votes.add(new Vote(0,0,false));
      }
      n.setVotes(votes);
      nuggets.add(n);
    }
    
		if ( XML.TOPIC.toString().equalsIgnoreCase(qName) ) {
		  Topic topic = new Topic(-1, id, atype, qSrc, qTrg, nSrc, nTrg, null, title);
		  topic.setAnswers(answers);
		  topic.setNuggets(nuggets);
			result.put(id, topic);
			id = null;
			atype = null;
			qSrc = null;
			qTrg = null;
			nSrc = null;
			nTrg = null;
			title = null;
			answers = new ArrayList<Answer>();
		  nuggets = new ArrayList<Nugget>();
		  tempDocid = null;
		  tempId = null;
		  tempVital = 0;
		  tempNonVital = 0;
		}
    if ( XML.QUESTION.toString().equalsIgnoreCase(qName) || XML.DESCRIPTION.toString().equalsIgnoreCase(qName) ) {
      inQuestion = false;
    }
    if ( XML.NARRATIVE.toString().equalsIgnoreCase(qName) ) {
      inNarrative = false;
    }
    if ( XML.ANSWERTYPE.toString().equalsIgnoreCase(qName) ) {
      inAtype = false;
    }
    if ( XML.TEXT.toString().equalsIgnoreCase(qName) ) {
      inAnswerText = false;
    }
    if ( XML.NUGGET.toString().equalsIgnoreCase(qName) ) {
      inAnswerNugget = false;
    }
	}
	
	public TopicSet getParsed() {
	  return result;
	}
	
}
